package com.olap.starter.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.olap.starter.config.dataSource.JdbcTemplateSource;
import com.olap.starter.core.base.BaseService;
import com.olap.starter.core.response.Result;
import com.olap.starter.core.response.ResultUtil;
import com.olap.starter.mapper.OnlineDictDataMapper;
import com.olap.starter.pojo.OnlineDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典数据表 服务实现类
 * </p>
 *
 * @author WBG
 * @since 2022-11-28
 */
@Service
@DS("online")
public class OnlineDictDataServices extends BaseService<OnlineDictDataMapper, OnlineDictData> {
    @Autowired
    private JdbcTemplateSource jdbcTemplateSource;

    @Override
    public List<OnlineDictData> list(Wrapper<OnlineDictData> queryWrapper) {
        LambdaQueryWrapper<OnlineDictData> queryWrappers = (LambdaQueryWrapper<OnlineDictData>) queryWrapper;
        queryWrappers.orderByAsc(OnlineDictData::getDictSort);
        return super.list(queryWrappers);
    }

    public Result initDist() {
        List<OnlineDictData> list = super.list(
                new LambdaQueryWrapper<OnlineDictData>()
                        .select(OnlineDictData::getDictCode, OnlineDictData::getDictValue,
                                OnlineDictData::getDictLabel
                                , OnlineDictData::getDictSort
                                , OnlineDictData::getDictType
                                , OnlineDictData::getCssClass
                                , OnlineDictData::getIsDefault
                                , OnlineDictData::getListClass
                                , OnlineDictData::getStatus).eq(OnlineDictData::getStatus,'0')
                        .orderByAsc(OnlineDictData::getDictType, OnlineDictData::getDictSort)
        );
        Map<String, List<OnlineDictData>> map = new HashMap<>();
        for (OnlineDictData onlineDictData : list) {
            List<OnlineDictData> orDefault = map.getOrDefault(onlineDictData.getDictType(), new ArrayList<>());
            orDefault.add(onlineDictData);
            map.put(onlineDictData.getDictType(), orDefault);
        }
        toAddCloud(map);
        return ResultUtil.success(map);
    }

    private  void toAddCloud(Map<String, List<OnlineDictData>> map){
        //嵌套微服务
        JdbcTemplate jdbcTemplate = jdbcTemplateSource.getJdbcTemplateByDbName("olp_cloud");
        if(jdbcTemplate != null){
            List<Map<String, Object>> maps = jdbcTemplate.queryForList("select concat(DICT_TYPE,\"_\",value) as DICT_CODE,SORT,VALUE,LABEL,DICT_TYPE,CSS_CLASS from system_dict_data where status = 0 order by dict_type,sort");
            for (Map<String, Object> obj : maps) {
                OnlineDictData onlineDictData = new OnlineDictData();
                if(obj.get("DICT_CODE") != null) {
                    onlineDictData.setDictCode(obj.get("DICT_CODE").toString());
                }
                if(obj.get("SORT") != null){
                    onlineDictData.setDictSort(Integer.parseInt(obj.get("SORT").toString()));
                }
                if(obj.get("LABEL") != null){
                    onlineDictData.setDictLabel(obj.get("LABEL").toString());
                }
                if(obj.get("VALUE") != null){
                    onlineDictData.setDictValue(obj.get("VALUE").toString());
                }
                if(obj.get("DICT_TYPE") != null){
                    onlineDictData.setDictType(obj.get("DICT_TYPE").toString());
                }
                if(obj.get("CSS_CLASS") != null){
                    onlineDictData.setCssClass(obj.get("CSS_CLASS").toString());
                }
                List<OnlineDictData> orDefault = map.getOrDefault(onlineDictData.getDictType(), new ArrayList<>());
                orDefault.add(onlineDictData);
                map.put(onlineDictData.getDictType(), orDefault);
            }
        }
    }

    public Result listOnline() {
        List<OnlineDictData> list = getBaseMapper().listOnline( );
        Map<String, List<OnlineDictData>> map = new HashMap<>();
        for (OnlineDictData onlineDictData : list) {
            List<OnlineDictData> orDefault = map.getOrDefault(onlineDictData.getDictType(), new ArrayList<>());
            orDefault.add(onlineDictData);
            map.put(onlineDictData.getDictType(), orDefault);
        }
        return ResultUtil.success(map);
    }


    /**
     * 获取按钮权限
     * @return
     */
    public Result<List<String>> listPermi(){
        JdbcTemplate jdbcTemplate = jdbcTemplateSource.getJdbcTemplateByDbName("olp_cloud");
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(
                "select id, name, permission\n" +
                "from system_menu\n" +
                "where type = 3\n" +
                "  and permission != ''");
        List<String> list = new ArrayList<>();
        if(CollUtil.isNotEmpty(maps)){
            maps.forEach(map -> list.add(map.get("permission").toString()));
        }
        return ResultUtil.success(list);
    }
}
